package top.toptimus.dto.store;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.toptimus.dto.order.orderSync.orderCommit.OrderCommitDto;
import top.toptimus.dto.order.orderSync.orderCommit.OrderCommitFacade;
import top.toptimus.util.DateTimeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 销售确认DTO
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderSalesConfirmationDTO {
    private static final Logger logger = LoggerFactory.getLogger(OrderSalesConfirmationDTO.class);
    private String id; //表头id
    private String storeId; //门店id
    private String assistantId;//业务员id
    private String discount;//销售折让
    private double salePrice;//应付款
    private double payment;//实付
    private String paymentMethodId;//支付方式id
    private String guest;//客户名称
    private String salesSourceId;//销售来源id
    private String createDate;//创建日期

    private List<OrderCommitFacade> orderCommitFacades = new ArrayList<>();//分录

    public OrderSalesConfirmationDTO(
            String id,
            String storeId,
            String assistantId,
            String discount,
            double salePrice,
            double payment,
            String paymentMethodId,
            String guest,
            String salesSourceId,
            String createDate
    ) {
        this.id = id;
        this.storeId = storeId;
        this.assistantId = assistantId;
        this.discount = discount;
        this.salePrice = salePrice;
        this.payment = payment;
        this.paymentMethodId = paymentMethodId;
        this.guest = guest;
        this.salesSourceId = salesSourceId;
        this.createDate = createDate;
    }

    public void append(OrderCommitDto orderCommitDto, SalesConfirmationEntryDTO salesConfirmationEntryDTO) {
        this.orderCommitFacades.add(new OrderCommitFacade(orderCommitDto, salesConfirmationEntryDTO));
    }

    /**
     * 销售分录数据清洗
     */
    public OrderSalesConfirmationDTO buildIds() {
        this.orderCommitFacades.forEach(orderCommitFacade -> {
                    {
                        orderCommitFacade.getSalesConfirmationEntryDTO()
                                .buildTitleId(this.id)  //  表头id
                                .buildOrderId(orderCommitFacade.getOrderCommitDto().getOrderId()); //  订单id
                    }
                }
        );
        return this;
    }

    /**
     * 补充订单信息
     *
     * @param shopId   店id
     * @param shopName 店名
     * @return this
     */
    public OrderSalesConfirmationDTO buildOrderInfo(String shopId, String shopName) {
        this.orderCommitFacades.forEach(
                orderCommitFacade ->
                        orderCommitFacade.getOrderCommitDto().buildOrderInfo(shopId, shopName)
        );
        return this;
    }

    /**
     * 坚山下单用
     *
     * @param orderCommitDtos orderCommitDtos
     */
    public OrderSalesConfirmationDTO(List<OrderCommitDto> orderCommitDtos) {
        this.id = UUID.randomUUID().toString();
        this.createDate = DateTimeUtil.getCurrentDateTime();
        orderCommitDtos.forEach(
                orderCommitDto -> {
                    this.orderCommitFacades.add(
                            new OrderCommitFacade(
                                    orderCommitDto,
                                    new SalesConfirmationEntryDTO(orderCommitDto).buildTitleId(this.id)
                            )
                    );
                })
        ;
    }
}
